import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Admin from '../views/Admin.vue'
import Order from '../components/order/Order.vue'
import Food from '../components/order/Food.vue'
import OrderData from '../components/admin/OrderData.vue'
import DishData from '../components/admin/DishData.vue'
import UserAdmin from '../components/admin/UserAdmin.vue'
import DishAdmin from '../components/admin/DishAdmin.vue'

import { useStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', name: 'login', component: Login },
    { path: '/home', component: Home,
       children:[
        { path:'food', component:Food},
        { path:'order',  component:Order },
        { path:'', redirect:'/home/food' }
      ]
    },
    { path: '/admin', component: Admin,
      children:[        
        { path:'orderData', component:OrderData},
        { path:'dishData', component:DishData},
        { path:'userAdmin', component:UserAdmin},
        { path:'dishAdmin', component:DishAdmin},
        { path:'',redirect:'/admin/orderData' }
      ]
    }
  ]
})


router.beforeEach((to,from,next)=>{
  const store = useStore()
  
	let token=store.token
  //console.log(token)
  if(!to.path.includes('login')&&!to.path.includes('assets')&&token===''){
    next('/login')
  }

  next()

})
export default router
